package com.fenshi.manager.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.bean.OrderBean;
import com.fenshi.manager.R;
import com.fenshi.presenter.RentOrBackPresenter;
import com.fenshi.presenter.imp.RentOrBackPresenterImp;
import com.fenshi.until.CookicUntil;
import com.fenshi.view.RentOrBackView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;

import butterknife.ButterKnife;
import butterknife.InjectView;
import zxing.activity.CaptureActivity;

/**
 * 申请租车订单详情
 */
public class RentActivity extends BaseActivity implements View.OnClickListener, RentOrBackView {

    View order_erweima_detail_linear;
    Button unlock_button, back_button;
    AlertDialog rent, back;
    ProgressDialog loading;
    RentOrBackPresenter presenter;
    @InjectView(R.id.order_car_type)
    TextView orderCarType;
    @InjectView(R.id.order_user)
    TextView orderUser;
    @InjectView(R.id.order_create_time)
    TextView orderCreateTime;
    @InjectView(R.id.car)
    TextView car;

    @Override
    public int getLayoutID() {
        return R.layout.activity_rent;
    }

    @Override
    public void initView() {
        presenter.initViewAndEvent();
    }

    @Override
    public void initActionbar() {

    }

    @Override
    public void initData() {
        presenter = new RentOrBackPresenterImp(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_erweima_detail_linear:
                Intent intent = new Intent(RentActivity.this, CaptureActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(CookicUntil.FROM, "rentActivty");
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rent_unlock:
                rent.show();
                break;
            case R.id.rent_back:
                back.show();
                break;
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        if (loading != null && !loading.isShowing()) loading.show();
    }

    @Override
    public void dimissLoad() {
        if (loading != null && loading.isShowing()) loading.dismiss();
    }

    @Override
    public void showRentOrReturnAlert() {
        if (rent != null) rent.show();
    }

    @Override
    public void showBackOrder() {
        if (back != null) back.show();
    }

    @Override
    public void setText(String msg) {
        unlock_button.setText(msg);
    }

    @Override
    public void checkCard(boolean isVisible) {

    }

    @Override
    public void initViewAndData(OrderBean bean) {
        order_erweima_detail_linear = findView(R.id.order_erweima_detail_linear);
        unlock_button = findView(R.id.rent_unlock);
        back_button = findView(R.id.rent_back);

        order_erweima_detail_linear.setOnClickListener(this);
        unlock_button.setOnClickListener(this);
        back_button.setOnClickListener(this);


        rent = new AlertDialog.Builder(RentActivity.this).setTitle("解锁确认").setMessage("点击确定后解锁车辆").setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                finish();
            }
        }).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
            }
        }).create();
        back = new AlertDialog.Builder(RentActivity.this).setTitle("打回订单").setMessage("确认取消当前订单").setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                finish();
            }
        }).create();
        AlertHelper helper = new AlertHelper(this);
        loading = helper.LoadingAlert();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
